﻿using System;
using System.Web.Mvc;
using APMS.WebSite.Common;
using APMS.WebSite.Models.ViewData;
using APMS.WebSite.Models.ViewData.UserControls;
using APMS.WebSite.Models.DataStore;

namespace APMS.WebSite.Controllers
{
    /// <summary>
    /// BaseControoler encapsulate common properties/methods required by all Controllers
    /// </summary>
    /// <version>$Revision$ $Date$</version>
    /// <history>
    /// <record>09/15/2008. raQuiam - Created this file</record>
    /// </history>
    [HandleError]
    public abstract class BaseController: Controller
    {
        #region Keys for ViewData
        /// <summary>
        /// Use in method SetTitle
        /// </summary>
        public const string TITLE = "Title";

        /// <summary>
        /// Use in method InitializeAction
        /// </summary>
        public const string CONTROL_PANEL = "ControlPanel";

        /// <summary>
        /// Use in method InitializeAction
        /// </summary>
        public const string MAIN_NAV = "MainNav";

        /// <summary>
        /// Key for retrieve error message in ViewData
        /// </summary>
        public const string KEY_ERROR_MSG = "ErrorMsg";

        /// <summary>
        /// Key for retrieve success message in ViewData
        /// </summary>
        public const string KEY_SUCCESS_MSG = "SuccessMsg";
        #endregion

        #region Helper Methods
        /// <summary>
        /// Populate all data required by MasterPage, redirect to Login page if UserProfile is null
        /// for any circumstance.
        /// 
        /// If everything work fine, this method will return null
        /// 
        /// Note: This method MUST BE CALLED for every Get method
        /// </summary>
        /// <returns></returns>
        protected ActionResult InitializeAction()
        {
            //// 1. Verify user session is expired or not, if so redirect to login page
            //if(String.IsNullOrEmpty(UserProfile.UserName))
            //    return RedirectToAction("Login", "User");

            //// 2. Populate data for ControlPanel user control
            //ControlPanelViewData cPanelViewData = new ControlPanelViewData();
            //cPanelViewData.UserName = UserProfile.UserName;
            //ViewData[CONTROL_PANEL] = cPanelViewData;

            //return null;
            return InitializeAction(MainNavItemType.NotSet);
        }

        /// <summary>
        /// Populate all data required by MasterPage, redirect to Login page if UserProfile is null
        /// for any circumstance.
        /// 
        /// If everything work fine, this method will return null
        /// 
        /// Note: This method MUST BE CALLED for every Get method
        /// </summary>
        /// <param name="mainNavItemType"></param>
        /// <returns></returns>
        protected ActionResult InitializeAction(MainNavItemType mainNavItemType)
        {
            // 1. Verify user session is expired or not, if so redirect to login page
            if (String.IsNullOrEmpty(UserProfile.UserName))
                return RedirectToAction("Login", "User");

            // 2. Populate data for ControlPanel user control
            ControlPanelViewData cPanelViewData = new ControlPanelViewData();
            cPanelViewData.UserName = UserProfile.UserName;
            ViewData[CONTROL_PANEL] = cPanelViewData;

            // 3. Set associated Main Menu Item
            if (mainNavItemType != MainNavItemType.NotSet)
            {
                MainNavViewData mainNavViewData = new MainNavViewData();
                mainNavViewData.CurrentMenu = mainNavItemType;
                ViewData[MAIN_NAV] = mainNavViewData;
            }

            return null;
        }

        protected void SetInfoToUserControls(Project currentProject)
        {
            MainNavViewData mainNavViewData = ViewData[MAIN_NAV] as MainNavViewData;
            if (mainNavViewData != null)
            {
                // Pass by reference
                mainNavViewData.ProjectID = currentProject.ProjectID;
            }
        }

        /// <summary>
        /// Set title which required by Master Page
        /// </summary>
        /// <param name="title">title which indicate the action that user is doing (most of the case is View Name)</param>
        protected void SetTitle(string title)
        {
            SetTitle(title, false);
        }

        protected void SetTitle(string title, bool isReplace)
        {
            if (isReplace)
            {
                ViewData[TITLE] = title;
            }
            else
            {
                ViewData[TITLE] = String.Format("Agile Project Management System | {0}", title);
            }
        }
        #endregion
    }
}
